﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace UILibrary
{
    //provide support of thread-safe operations
    public class ThreadSafeUIOperations
    {
        internal delegate void BoolPropertyDelegate(Control ctrl, bool Val);
        internal delegate void TextPropertyDelegate(Control ctrl, string Val);
        internal delegate void ForeColorPropertyDelegate(Control ctrl, Color Val);
        internal delegate void ListBoxItemDelegate(CheckedListBox Ctrl, string Msg, bool CheckState);
        internal delegate void Int32TabPropertyDelegate(TabControl ctrl, Int32 Val);

        private void SetVisibleThreadSafe(Control Ctrl, bool Val)
        { Ctrl.Visible = Val; }

        private void SetEnabledThreadSafe(Control Ctrl, bool Val)
        { Ctrl.Enabled = Val; }

        private void SetTextThreadSafe(Control Ctrl, string Val)
        { Ctrl.Text = Val; }

        private void SetForeColorThreadSafe(Control Ctrl, Color Val)
        { Ctrl.ForeColor = Val; }

        private void AddListBoxItemThreadSafe(CheckedListBox Ctrl, string Msg, bool CheckState)
        { Ctrl.Items.Add(Msg, false); }

        private void SetTabThreadSafe(TabControl Ctrl, Int32 Val)
        { Ctrl.SelectTab(Val); }

        public void SetVisible(Control Ctrl, bool Val)
        {
            if (Ctrl.InvokeRequired)
            {
                BoolPropertyDelegate Obj = new BoolPropertyDelegate(this.SetVisibleThreadSafe);
                Ctrl.BeginInvoke(Obj, new object[] { Ctrl, Val });
            }
            else
                SetVisibleThreadSafe(Ctrl, Val);
        }

        public void SetEnabled(Control Ctrl, bool Val)
        {
            if (Ctrl.InvokeRequired)
            {
                BoolPropertyDelegate Obj = new BoolPropertyDelegate(this.SetEnabledThreadSafe);
                Ctrl.BeginInvoke(Obj, new object[] { Ctrl, Val });
            }
            else
                SetEnabledThreadSafe(Ctrl, Val);
        }

        public void SetText(Control Ctrl, string Val)
        {
            if (Ctrl.InvokeRequired)
            {
                TextPropertyDelegate Obj = new TextPropertyDelegate(this.SetTextThreadSafe);
                Ctrl.BeginInvoke(Obj, new object[] { Ctrl, Val });
            }
            else
                SetTextThreadSafe(Ctrl, Val);
        }

        public void SetForeColor(Control Ctrl, Color Val)
        {
            if (Ctrl.InvokeRequired)
            {
                ForeColorPropertyDelegate Obj = new ForeColorPropertyDelegate(this.SetForeColorThreadSafe);
                Ctrl.BeginInvoke(Obj, new object[] { Ctrl, Val });
            }
            else
                SetForeColorThreadSafe(Ctrl, Val);
        }

        public void AddListBoxItem(CheckedListBox Ctrl, string Msg, bool CheckState)
        {
            if (Ctrl.InvokeRequired)
            {
                ListBoxItemDelegate Obj = new ListBoxItemDelegate(this.AddListBoxItemThreadSafe);
                Ctrl.BeginInvoke(Obj, new object[] { Ctrl, Msg, false });
            }
            else
                AddListBoxItemThreadSafe(Ctrl, Msg, false);
        }

        public void SetTab(TabControl Ctrl, Int32 Val)
        {
            if (Ctrl.InvokeRequired)
            {
                Int32TabPropertyDelegate Obj = new Int32TabPropertyDelegate(this.SetTabThreadSafe);
                Ctrl.BeginInvoke(Obj, new object[] { Ctrl, Val});
            }
            else
                SetTabThreadSafe(Ctrl, Val);
        }

    }
}
